#include "teamform.h"
#include "ui_teamform.h"

#include <QPainter>
#include <QPixmap>
#include "../wsim_client.h"
#include <QMouseEvent>
#include <QDebug>

TeamForm::TeamForm(int ffd,int magic,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TeamForm)
{
    ui->setupUi(this);
    m_ffd=ffd;
    m_magic=magic;
    m_barray.clear();
}

TeamForm::~TeamForm()
{
    delete ui;
}
void TeamForm::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    if(!m_pix.isNull())
        painter.drawPixmap(0,0,this->width(),this->height(),m_pix);
}
void TeamForm::appByte(QByteArray barray)
{
    if(barray.size()>0)
    {
        m_barray.append(barray);
    }
}
void TeamForm::newPixmap()
{
    loadByteToPixmap();
    m_barray.clear();
}

void TeamForm::loadByteToPixmap()
{
    if(m_barray.size()>0)
    {
        m_pix.loadFromData(m_barray);
        this->setFixedSize(m_pix.size());

        this->repaint();
    }
}
void TeamForm::closeEvent(QCloseEvent *event)
{
    WSIM_client::sendTeamCommand(m_ffd,m_magic,"closeTeam");
}
void TeamForm::mouseMoveEvent(QMouseEvent *event)
{
    //qDebug()<<"x"<<this->x()<<","<<this->geometry().x()<<","<<event->x();
    //qDebug()<<"y"<<this->y()<<","<<this->geometry().y()<<","<<event->y();
    int x=event->x();
    if(x<0) x=0;
    if(x>this->width()) x=this->width();
    int y=event->y();
    if(y<0) y=0;
    if(y>this->height()) y=this->height();
    QString button;
    if(event->button()==Qt::RightButton) button="RightButton";
    else if(event->button()==Qt::LeftButton) button="LeftButton";
    else if(event->button()==Qt::MidButton) button="MidButton";
    QString command=QString("mouseMove(%1,%2,%3)").arg(x).arg(y).arg(button);

    WSIM_client::sendTeamCommand(m_ffd,m_magic,command.toStdString());
}
void TeamForm::mousePressEvent(QMouseEvent *event)
{
    int x=event->x();
    if(x<0) x=0;
    if(x>this->width()) x=this->width();
    int y=event->y();
    if(y<0) y=0;
    if(y>this->height()) y=this->height();
    QString button;
    if(event->button()==Qt::RightButton) button="RightButton";
    else if(event->button()==Qt::LeftButton) button="LeftButton";
    else if(event->button()==Qt::MidButton) button="MidButton";
    QString command=QString("mousePress(%1,%2,%3)").arg(x).arg(y).arg(button);
    WSIM_client::sendTeamCommand(m_ffd,m_magic,command.toStdString());
    //qDebug()<<"x:"<<event->x()<<",posx:"<<event->pos().x()<<",globalx:"<<event->globalX();
}
void TeamForm::mouseReleaseEvent(QMouseEvent *event)
{
    int x=event->x();
    if(x<0) x=0;
    if(x>this->width()) x=this->width();
    int y=event->y();
    if(y<0) y=0;
    if(y>this->height()) y=this->height();
    QString button;
    if(event->button()==Qt::RightButton) button="RightButton";
    else if(event->button()==Qt::LeftButton) button="LeftButton";
    else if(event->button()==Qt::MidButton) button="MidButton";
    QString command=QString("mouseRelease(%1,%2,%3)").arg(x).arg(y).arg(button);
    WSIM_client::sendTeamCommand(m_ffd,m_magic,command.toStdString());

}
void TeamForm::mouseDoubleClickEvent(QMouseEvent *event)
{
    int x=event->x();
    if(x<0) x=0;
    if(x>this->width()) x=this->width();
    int y=event->y();
    if(y<0) y=0;
    if(y>this->height()) y=this->height();
    QString button;
    if(event->button()==Qt::RightButton) button="RightButton";
    else if(event->button()==Qt::LeftButton) button="LeftButton";
    else if(event->button()==Qt::MidButton) button="MidButton";
    QString command=QString("mouseDoubleClick(%1,%2,%3)").arg(x).arg(y).arg(button);
    WSIM_client::sendTeamCommand(m_ffd,m_magic,command.toStdString());

}
void TeamForm::keyPressEvent(QKeyEvent *ev)
{

        QString command=QString("keyButton(%1,%2)").arg(ev->key()).arg(ev->modifiers());
        WSIM_client::sendTeamCommand(m_ffd,m_magic,command.toStdString());

}
